package org.ruiyichen.guizhou.agr.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.gson.reflect.TypeToken;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.base.BaseActivity;
import org.ruiyichen.guizhou.agr.base.DataManger;
import org.ruiyichen.guizhou.agr.bean.Response;
import org.ruiyichen.guizhou.agr.bean.mode.IIrrigatedAreaModel;
import org.ruiyichen.guizhou.agr.bean.mode.IIrrigatedDeviceModel;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.imp.IIrrigatedAreaFilter;
import org.ruiyichen.guizhou.agr.imp.IIrrigatedDeviceFilter;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.ui.adapter.DeviceControlRecyclerAdapter;
import org.ruiyichen.guizhou.agr.ui.listener.OnRecyclerItemClick;
import org.ruiyichen.guizhou.agr.ui.mineview.BToast;
import org.ruiyichen.guizhou.agr.utils.ListUtil;
import org.ruiyichen.guizhou.agr.utils.StrUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Administrator on 2018/9/8.
 */

public class IrrigatedDeviceControlActivity extends BaseActivity implements View.OnClickListener {
    public static final String TAG_ID = "id";
    public static final String PAGE_TITLE = "title";

    private RecyclerView rv_nf_info_root;

    private DeviceControlRecyclerAdapter adapter;

    private List<IIrrigatedDeviceModel> list = new ArrayList<>();

    private IIrrigatedAreaModel curIrrigatedAreaModel;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.df_activity_irrigated_device_control);
        curIrrigatedAreaModel = (IIrrigatedAreaModel) getIntent().getSerializableExtra(Constant.ParamKey.IRRIGATED_AREA);
        bOkHttpManager = new OkHttpManager(this, bHandler);
        initData();
    }

    @Override
    protected void initViews() {
        setTitle(getIntent().getStringExtra(PAGE_TITLE)).setRightImage(R.drawable.df_image_add_plan);
        rv_nf_info_root = findViewById(R.id.rv_home_control);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setAutoMeasureEnabled(true);
        rv_nf_info_root.setLayoutManager(manager);
        rv_nf_info_root.setNestedScrollingEnabled(false);

        list.add(new IIrrigatedDeviceModel(false, "1#设备", true));
        list.add(new IIrrigatedDeviceModel(false, "2#设备", false));

        adapter = new DeviceControlRecyclerAdapter(this);
        adapter.setmDatas(list);
        rv_nf_info_root.setAdapter(adapter);
    }

    @Override
    protected void initEvents() {
        findViewById(R.id.iv_normal_back).setOnClickListener(this);
        findViewById(R.id.tv_device_all_open).setOnClickListener(this);
        findViewById(R.id.tv_device_all_close).setOnClickListener(this);
        findViewById(R.id.tv_normal_other).setOnClickListener(this);
        adapter.setOnRecyclerItemClick(new OnRecyclerItemClick() {
            @Override
            public void onItemClick(View view, int position) {
                if (view.getId() == R.id.iv_irrigated_device_switch) {
                    //点击设备开关
                    boolean isOpen = list.get(position).isIControlOpen();
                    //TODO 调用接口去开启关闭
                } else {

                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_normal_back:
                onBackPressed();
                break;
            case R.id.tv_device_all_open:
                //TODO
                for (IIrrigatedDeviceModel model : list) {
                    model.controlOpen = true;
                }
                adapter.notifyDataSetChanged();
                break;
            case R.id.tv_device_all_close:
                //TODO
                for (IIrrigatedDeviceModel model : list) {
                    model.controlOpen = false;
                }
                adapter.notifyDataSetChanged();
                break;
            case R.id.tv_normal_other:
                //灌溉计划
                Intent it = new Intent(this, IrrigatedDevicePlanActivity.class);
                it.putExtra(IrrigatedDevicePlanActivity.TAG_ID, getIntent().getStringExtra(TAG_ID));
                it.putExtra(Constant.ParamKey.IRRIGATED_AREA, curIrrigatedAreaModel);
                startActivity(it);
                break;
        }
    }

    private void initData() {
        if (StrUtil.isNotEmpty(getIntent().getStringExtra(TAG_ID))) {
            Map<String, String> body = new HashMap<>();
            body.put("irrigId", getIntent().getStringExtra(TAG_ID));
            bOkHttpManager.httpRequest(Constant.RequestType.GET_DEVICES_BY_IRRIGID, Constant.Http.GET,
                    body, false, new
                            TypeToken<Response<List<IIrrigatedDeviceModel>, Void>>() {
                            }.getType(), true, false);
        }
    }


    /**
     * 提交
     */
    private void submit() {
        Map<String, String> body = new HashMap<>();
//        token	String	登录成功返回的token
        body.put("token", DataManger.getInstance().getUserInfo().token);

        bOkHttpManager.httpRequest(Constant.RequestType.SEND_MESSAGE, Constant.Http.POST, body, false, new
                TypeToken<Response<Void, Void>>() {
                }.getType(), true, false);
    }

    @Override
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {
        super.onPostHandle(requestType, response, error, errorCode, obj1, obj2);
        if (error) {
            BToast.getInstance().show(getResources().getString(R.string.abnormal));
        } else {
            if (response == null) {
                BToast.getInstance().show(getResources().getString(R.string.datafailure));
            } else {
                if (requestType == Constant.RequestType.GET_DEVICES_BY_IRRIGID) {
                    Response<List<IIrrigatedDeviceModel>, Void> result = (Response<List<IIrrigatedDeviceModel>, Void>) response;
                    if (result.isSuccess()) {
                        list.clear();
                        if (ListUtil.isNotEmpty(result.getData())) {
                            list.addAll(result.getData());
                        }
                        adapter.notifyDataSetChanged();
                    } else {
                        BToast.getInstance().show(result.getMsg());
                    }
                }
            }
        }
    }

}
